package com.zmn.plat.admin.controller.plat;

import static java.util.stream.Collectors.groupingBy;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.KvDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.PlatEnum;
import com.zmn.plat.model.entity.servcategory.ServAssCategory;
import com.zmn.plat.model.entity.servcategory.ServAssCategoryQuery;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.model.vo.servcategory.ServAssCategoryVO;
import com.zmn.plat.services.interfaces.category.BaseCategoryService;
import com.zmn.plat.services.interfaces.servcategory.ServAssCategoryService;
import com.zmn.plat.services.interfaces.servcategory.ServCategoryService;

/**
 * 渠道BaseController
 *
 * @author xiangjunming
 * @since 2019 /08/19 19:30:36
 */
public class PlatServCategoryBaseController extends PlatBaseController {

    /**
     * The Serv ass category service.
     */
    @Autowired
    protected ServAssCategoryService servAssCategoryService;

    @Autowired
    protected ServCategoryService servCategoryService;

    @Autowired
    protected BaseCategoryService baseCategoryService;

    /**
     * 获取全部分类信息
     *
     * @param setQuery
     *            showType 产品分类 1后台 2前台
     * @return the list
     */
    @SuppressWarnings("Duplicates")
    protected List<KvDTO<Integer, String>> getAllChildCategory(Consumer<ServAssCategoryQuery> setQuery) {
        ServAssCategoryQuery categoryQuery = new ServAssCategoryQuery();
        categoryQuery.setCategOneStatus(GlobalConsts.YES);
        categoryQuery.setCategTwoStatus(GlobalConsts.YES);
        setQuery.accept(categoryQuery);

        if (ProductConsts.EC_PRODUCT_TYPE.equals(categoryQuery.getShowType())) {
            categoryQuery.setServCategShowStatus(GlobalConsts.YES);
        } else {
            categoryQuery.setServCategStatus(GlobalConsts.YES);
        }

        List<ServAssCategoryVO> voList = servAssCategoryService.listByQuery(categoryQuery);
        // 按服务分类分组
        Map<Integer, List<ServAssCategoryVO>> serviceCategoryMap = voList.stream().collect(groupingBy(ServAssCategory::getServCategId));

        List<KvDTO<Integer, String>> ret = serviceCategoryMap.values().stream().map(serviceCategory -> {
            // 按一级分类分组
            Map<Integer, List<ServAssCategoryVO>> categoryOneMap =
                serviceCategory.stream().collect(groupingBy(ServAssCategory::getCategOneId));

            List<KvDTO> categoryOneList = categoryOneMap.values().stream().map(categoryOne -> {
                // 二级分类KvDTO
                List<KvDTO> categList =
                    categoryOne.stream().map(p -> new KvDTO(p.getCategId(), p.getCategName())).collect(Collectors.toList());
                // 一级分类KvDTO
                KvDTO categOne = new KvDTO<>(categoryOne.get(0).getCategOneId(), categoryOne.get(0).getCategOneName());
                categOne.setChildren(categList);
                return (KvDTO<Integer, String>)categOne;
            }).collect(Collectors.toList());

            // 服务分类KvDTO
            KvDTO dto = new KvDTO<>(serviceCategory.get(0).getServCategId(), serviceCategory.get(0).getServCategName());
            dto.setChildren(categoryOneList);
            return (KvDTO<Integer, String>)dto;
        }).collect(Collectors.toList());
        return ret;
    }

    /**
     * 获取服务分类
     *
     * @return the list
     */
    @ReadOnlyConnection
    protected List<ServCategory> getServCategoryList() {
        ServCategory query = new ServCategory();
        query.setStatus(GlobalDict.STATUS_ENABLE);
        return servCategoryService.listByQuery(query);
    }

    protected Integer getRealShowPlat(Integer plat) {
        boolean zmnPlat = StaffAdminService.isPermitted("/plat/serv/category/zmnServCategory.action");
        boolean yeyxPlat = StaffAdminService.isPermitted("/plat/serv/category/yeyxServCategory.action");
        boolean cnhbPlat = StaffAdminService.isPermitted("/plat/serv/category/cnhbServCategory.action");
        boolean ybwxPlat = StaffAdminService.isPermitted("/plat/serv/category/ybwxServCategory.action");

        boolean flag = false;
        Integer realPlat = 0;

        if (Objects.equals(plat, PlatEnum.ZMN.value) && zmnPlat) {
            realPlat = PlatEnum.ZMN.value;
        } else if (Objects.equals(plat, PlatEnum.YEYX.value) && yeyxPlat) {
            realPlat = PlatEnum.YEYX.value;
        } else if (Objects.equals(plat, PlatEnum.CNHB.value) && cnhbPlat) {
            realPlat = PlatEnum.CNHB.value;
        } else if (Objects.equals(plat, PlatEnum.YBSH.value) && ybwxPlat) {
            realPlat = PlatEnum.YBSH.value;
        }

        if (Objects.equals(realPlat, 0)) {
            if (zmnPlat) {
                return PlatEnum.ZMN.value;
            }
            if (yeyxPlat) {
                return PlatEnum.YEYX.value;
            }
            if (cnhbPlat) {
                return PlatEnum.CNHB.value;
            }
            if (ybwxPlat) {
                return PlatEnum.YBSH.value;
            }
        }

        return realPlat;
    }
}